package com.luceneheritrixbook.index;

import org.apache.lucene.document.*;

import com.luceneheritrixbook.core.Product;

public class ProductDocument {

	private static final String PRODUCT_ID = "productid";

	private static final String INDEX_TIME = "indextime";

	private static final String PRODUCT_URL = "producturl";

	private static final String CATEGORY = "category";

	private static final String PRODUCT_NAME = "name";

	private static final String PRODUCT_TYPE = "type";

	public static Document buildProductDocument(Product product, int id) {

		Document doc = new Document();

		Field identifier = new Field(PRODUCT_ID, id + "", Field.Store.YES,
				Field.Index.UN_TOKENIZED);

		long mills = System.currentTimeMillis();
		Field indextime = new Field(INDEX_TIME, mills + "", Field.Store.YES,
				Field.Index.UN_TOKENIZED);

		Field producturl = new Field(PRODUCT_URL, product.getOriginalUrl(),
				Field.Store.YES, Field.Index.UN_TOKENIZED);

		Field category = new Field(CATEGORY, product.getCategory(),
				Field.Store.YES, Field.Index.TOKENIZED);

		Field name = new Field(PRODUCT_NAME, product.getName(),
				Field.Store.YES, Field.Index.TOKENIZED);

		Field type = new Field(PRODUCT_TYPE, product.getType(),
				Field.Store.YES, Field.Index.TOKENIZED);

		String text = product.getCategory();
		text += " " + product.getName();
		text += " " + product.getType();
		Field all = new Field("all", text, Field.Store.YES,
				Field.Index.TOKENIZED);

		// add all
		doc.add(identifier);
		doc.add(indextime);
		doc.add(producturl);
		doc.add(category);
		doc.add(name);
		doc.add(type);
		doc.add(all);

		return doc;

	}

}
